import 'package:common_utils/common_utils.dart';
import 'package:flutter_deer/database/beans/get_all_goods_list.dart';
import 'package:flutter_deer/mvvm/base_net_provider.dart';
import 'package:flutter_deer/util/toast_utils.dart';
import 'package:flutter_deer/widgets/dialog/goods_selection_dialog.dart';
import 'package:get/get_rx/src/rx_types/rx_types.dart';

class GoodsSelectProvider extends BaseNetProvider {
  // 当前的商品分类选择
  final Rx<Category?> _selectCategory = Rx<Category?>(null);

  Category? get selectCategory => _selectCategory.value;

  // 已经选择的商品列表
  final RxList<GetAllGoodsList> _selectedGoods = <GetAllGoodsList>[].obs;

  List<GetAllGoodsList> get selectedGoods => _selectedGoods.value;

  // 商品分类列表
  final RxList<Category> _categoryList = <Category>[].obs;

  List<Category> get categoryList => _categoryList.value;

  // 商品列表
  final RxList<GetAllGoodsList> _allGoodsList = <GetAllGoodsList>[].obs;

  // 当前目录下的商品列表
  final RxList<GetAllGoodsList> _currentGoodsList = <GetAllGoodsList>[].obs;

  List<GetAllGoodsList> get currentGoodsList => _currentGoodsList.value;

  @override
  void onReady() {
    super.onReady();
    // 读取数据库中的商品列表
    _getAllGoods();
  }

  // 读取数据库中的商品列表
  Future<void> _getAllGoods() async {
    final goodsList = await GetAllGoodsList.getAllGoods();
    if (goodsList != null) {
      _allGoodsList.value = goodsList;
      _currentGoodsList.value = goodsList;
      // 构造商品分类列表
      final defaultCategory = Category(id: '', name: '全部');
      _categoryList.add(defaultCategory);
      _selectCategory.value = defaultCategory;
      for (final goods in goodsList) {
        if (!_categoryList.any((element) => element.id == goods.goodsCategoryId)) {
          _categoryList.add(Category(id: goods.goodsCategoryId, name: goods.goodsCategoryName));
          LogUtil.d('商品分类列表: size=${_categoryList.length}   ${goods.goodsCategoryId} ---> ${goods.goodsCategoryName}');
        }
      }
    }
  }

  // 切换商品分类
  void switchCategory(Category category) {
    _selectCategory.value = category;
    // 刷新商品列表
    _refreshGoodsList();
  }

  // 刷新商品列表
  void _refreshGoodsList() {
    if (_selectCategory.value != null) {
      if (_selectCategory.value?.id == '') {
        _currentGoodsList.value = _allGoodsList.value;
      } else {
        _currentGoodsList.value = _allGoodsList.value.where((element) => element.goodsCategoryId == _selectCategory.value?.id).toList();
      }
    }
  }

  // 清空商品
  void clearGoods() {
    _selectedGoods.value.clear();
  }

  // 计算总价
  double getTotalPrice() {
    double total = 0;
    // for (final goods in _selectedGoods.value) {
    //   total += goods.goodsPrice * goods.goodsCount;
    // }
    return total;
  }

  // 选择商品
  void selectGoods(GetAllGoodsList goods) {
    final index = _selectedGoods.value.indexWhere((element) => element.goodsId == goods.goodsId);
    if (index != -1) {
      _selectedGoods.value[index].count.value++;
    } else {
      goods.count.value = 1;
      _selectedGoods.value.add(goods);
    }
    _selectedGoods.refresh();
    print('选择商品: ${goods.goodsName} ---> ${_selectedGoods.length}');
  }

  // 移除商品
  void removeGoods(GetAllGoodsList goods) {
    print('移除商品: ${goods.goodsName} ---> ${_selectedGoods.length}');
    goods.count.value = 1;
    _selectedGoods.value.removeWhere((element) => element.goodsId == goods.goodsId);
    _selectedGoods.refresh();
    print('移除商品后: ${_selectedGoods.length}');
  }

  // 确认选择
  RxList<GetAllGoodsList> confirmSelect() {
    if (_selectedGoods.isEmpty) {
      Toast.show('没有选择任何商品');
      // return [];
    }
    for (final element in _selectedGoods) {
      print('确认选择: ${element.goodsName} ---> ${element.count.value}');
    }
    return _selectedGoods;
  }
}
